package com.org.devg.elasticsearch.wapper;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.*;

/**
 * 帮助生成和访问Json-List对象的帮助类 Created by luoyifan on 2016/3/30.
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public class JsonArrayWapper implements Collection<Object> {

  public final List innerList;

  public JsonArrayWapper(List innerList) {
    this.innerList = innerList;
  }

  public JsonArrayWapper appendObj(Object... args) {
    JsonWapper jw = new JsonWapper();
    jw.set(args);
    innerList.add(jw.getInnerMap());
    return this;
  }

  public JsonArrayWapper append(Object obj) {
    innerList.add(obj);
    return this;
  }


  public static List<Map<String, Object>> parseJSON2List(String jsonStr) {
    JSONArray jsonArr = JSONArray.fromObject(jsonStr);
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    Iterator<JSONObject> it = (Iterator<JSONObject>) jsonArr.iterator();
    while (it.hasNext()) {
      JSONObject json2 = it.next();
      list.add(parseJSON2Map(json2.toString()));
    }
    return list;
  }


  /**
   * 将json字符串转化成Map对象，主要是针对单个json对象
   */
  public static Map<String, Object> parseJSON2Map(String jsonStr) {
    Map<String, Object> map = new HashMap<String, Object>();
    // 最外层解析
    JSONObject json = JSONObject.fromObject(jsonStr);
    for (Object k : json.keySet()) {
      Object v = json.get(k);
      // 如果内层还是数组的话，继续解析
      if (v instanceof JSONArray) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Iterator<JSONObject> it = ((JSONArray) v).iterator();
        while (it.hasNext()) {
          JSONObject json2 = it.next();
          list.add(parseJSON2Map(json2.toString()));
        }
        map.put(k.toString(), list);
      } else {
        map.put(k.toString(), v);
      }
    }
    return map;
  }


  public Object get(int index) {
    return innerList.get(index);
  }

  @Override
  public int size() {
    return innerList.size();
  }

  @Override
  public boolean isEmpty() {
    return innerList.isEmpty();
  }

  @Override
  public boolean contains(Object o) {
    return innerList.contains(o);
  }

  @Override
  public Iterator<Object> iterator() {
    return innerList.iterator();
  }

  @Override
  public Object[] toArray() {
    return innerList.toArray();
  }

  @Override
  public Object[] toArray(Object[] a) {
    return innerList.toArray(a);
  }

  @Override
  public boolean add(Object o) {
    if (o instanceof JsonWapper) {
      return innerList.add(((JsonWapper) o).getInnerMap());
    }
    return innerList.add(o);
  }

  @Override
  public boolean remove(Object o) {
    return innerList.remove(o);
  }

  @Override
  public boolean containsAll(Collection<?> c) {
    return innerList.containsAll(c);
  }

  @Override
  public boolean addAll(Collection<?> c) {
    return innerList.addAll(c);
  }

  @Override
  public boolean removeAll(Collection<?> c) {
    return innerList.removeAll(c);
  }

  @Override
  public boolean retainAll(Collection<?> c) {
    return innerList.retainAll(c);
  }

  @Override
  public void clear() {
    innerList.clear();
  }
}
